#!/usr/bin/env python
# coding: utf-8

# In[1]:


exec(open('init_path.py').read())
exec((P_Lib/'GasStation.py').read_text())
get_ipython().run_line_magic('matplotlib', 'inline')


# In[2]:


YMD = read_hdf(P_Research / 'Others' / 'Datetime' / 'utils_dates_YMD.h5', 'utils')
dict_intYMD_to_dtYMD = YMD.set_index('intYMD').dtYMD.to_dict()
dict_dtYMD_to_dtYM = YMD.dtYM.to_dict()
dict_dtYMD_to_y2kW = YMD.y2kW.to_dict()
dict_y2kW_to_dtYMD = YMD[['dtYMD','y2kW']].drop_duplicates(subset=['y2kW']).set_index('y2kW').dtYMD.to_dict()


# In[12]:


YMD[['dtYMD','intYMD','y2kW']].to_stata(P_GS_Data / 'Other' / 'dates.dta', write_index=False)


# In[3]:


Holiday = read_excel(P_GS_Data / 'Other' / 'holiday_list_2014_2019.xlsx')
Holiday['Date'] = to_datetime((Holiday.Year*10000+Holiday.Date.dt.month*100+Holiday.Date.dt.day).astype(str))
ls_holiday = Holiday.loc[Holiday.Type.str.contains('national holiday', case=False), 'Date'].tolist()


# In[4]:


files = sorted(list((P_GS_Data_Raw / 'PH_Day').glob('*.h5')))
File = DataFrame([int(f.stem) for f in files], columns = ['intYMD'])
File['dtYMD'] = File.intYMD.map(dict_intYMD_to_dtYMD)
File['Holiday'] = File.dtYMD.isin(ls_holiday)
File['DoW'] = File.dtYMD.dt.dayofweek
File['y2kW'] = File.dtYMD.map(dict_dtYMD_to_y2kW)
File['Weekday'] = (File.DoW>=1) & (File.DoW<=5)
File.head(2)


# In[5]:


ls_strYMD_weekday_nonholiday = File.loc[(~File.Holiday) & File.Weekday, 'intYMD'].astype(str).tolist()


# In[6]:


save_obj(ls_strYMD_weekday_nonholiday, P_GS_Data / 'GS' / 'ls_strYMD_weekday_nonholiday.pkl')
# ls_strYMD_weekday_nonholiday = load_obj(P_GS_Data / 'GS' / 'ls_strYMD_weekday_nonholiday.pkl')


# In[9]:


File.to_hdf(P_GS_Data_Raw / 'PH_Day.h5', 'GS', mode='w', complevel=9, complib='blosc')
# File = read_hdf(P_GS_Data_Raw / 'PH_Day.h5', 'GS')
# File.head(2)


# In[ ]:




